//
//  PushTaskController.m
//  Safflower
//
//  Created by 袁亮 on 2016/11/29.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "PushTaskController.h"
#import "PushTaskView.h"
#import "PushTaskEditController.h"
#import "TaskDataCenter.h"
#import "SelectFriendController.h"
#import "SelectTaskDateController.h"
#import "ZZDatePickerView.h"
#import "TaskDateModel.h"

@interface PushTaskController ()<PushTaskViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,PushTaskEditDelegate,SelectFriendControllerDelegate,ZZDatePickerViewDelegate>

@property (nonatomic, strong) PushTaskView *pushTaskView;
@property (nonatomic, strong) UIImagePickerController *imagepickController;
@property (nonatomic, strong) TaskModel *taskModel;
@property (nonatomic, strong) TaskDateModel *dateModel;
@property (nonatomic, strong) FriendModel *friendModel;
@property (nonatomic, strong) TaskDataCenter *dataCenter;
@property (nonatomic, assign) TaskDateMode selectMode;

@end

@implementation PushTaskController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.baseTitle = @"发布任务";
    self.rightBtnTitle = @"返回";
    
    _taskModel = [TaskModel new];
    _dateModel = [TaskDateModel new];
    
    _dataCenter = [TaskDataCenter new];
    
    [self makePushTaskView];
    
    [self initImagePicker];
}

- (void)makePushTaskView
{
    _pushTaskView = [PushTaskView new];
    _pushTaskView.delegate = self;
    [self.baseView addSubview:_pushTaskView];
    [_pushTaskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}

#pragma mark --- 实例化相机
-(void)initImagePicker
{
    //绑定调用相册的协议方法
    if (self.imagepickController == nil) {
        self.imagepickController = [[UIImagePickerController alloc]init];
        [self.imagepickController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
        
        [self.imagepickController.navigationBar setTitleTextAttributes:
         @{NSFontAttributeName:[UIFont systemFontOfSize:17.0f],
           NSForegroundColorAttributeName:[UIColor blackColor]}];
        [self.imagepickController.navigationBar setTintColor:[UIColor blackColor]];
        self.imagepickController.delegate = self;
    }
}

- (void)clickTaskStartDate
{
    [[ZZDatePickerView sharedPicker] showPickerWithMode:TaskDateModeOfStart delegate:self];
}

- (void)clickTaskEndDate
{
    [[ZZDatePickerView sharedPicker] showPickerWithMode:TaskDateModeOfEnd delegate:self];
}

- (void)didSelectDatePickerWithMode:(TaskDateMode)mode date:(NSString *)date
{
    if (mode == TaskDateModeOfStart) {
        _dateModel.startDate = date;
        _pushTaskView.taskStartDateField.text = [ZZDateFormat fetch10Date:_dateModel.startDate format:@"yyyy年MM月dd日"];
    }else if (mode == TaskDateModeOfEnd) {
        _dateModel.endDate = date;
        _pushTaskView.taskEndDateField.text = [ZZDateFormat fetch10Date:_dateModel.endDate format:@"yyyy年MM月dd日"];;
    }
}

- (void)clickTaskFriend
{
    SelectFriendController *selectFriendController = [[SelectFriendController alloc]init];
    selectFriendController.delegate = self;
    [self presentViewController:selectFriendController animated:NO completion:nil];
}

- (void)didSelectIndexWithFriend:(FriendModel *)friendModel
{
    _friendModel = friendModel;
    _pushTaskView.taskFriendField.text = friendModel.remarks;
}



- (void)clickUploadGift
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择一张礼物图片" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        if (self.imagepickController != nil) {
            self.imagepickController.sourceType = UIImagePickerControllerSourceTypeCamera;
            self.imagepickController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            self.imagepickController.allowsEditing = YES;
            [self presentViewController:self.imagepickController animated:YES completion:nil];
        }
    }];
    UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        if (self.imagepickController != nil) {
            self.imagepickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            self.imagepickController.allowsEditing = YES;
            [self presentViewController:self.imagepickController animated:YES completion:nil];
            
        }
        
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:deleteAction];
    [alertController addAction:archiveAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage * image = nil;
    if(picker.allowsEditing == NO){
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }else{
        image = [info objectForKey:UIImagePickerControllerEditedImage];
    }
    if(image != nil){
        
        UIImage *scaleImage = [UIImage scaleImage:image multiple:0.25];
        
        NSData *imageData = UIImageJPEGRepresentation(scaleImage, 0.25);
        
        _pushTaskView.giftImage = [UIImage imageWithData:imageData];
        
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)clickTaskDescription:(NSString *)description
{
    PushTaskEditController *descriptionEditController = [[PushTaskEditController alloc]init];
    descriptionEditController.editType = TaskEditTypeOfDescription;
    descriptionEditController.text = description;
    descriptionEditController.delegate = self;
    [self presentViewController:descriptionEditController animated:NO completion:nil];
}

- (void)clickTaskSendWord:(NSString *)sendWord
{
    PushTaskEditController *sendwordEditController = [[PushTaskEditController alloc]init];
    sendwordEditController.editType = TaskEditTypeOfSendWord;
    sendwordEditController.text = sendWord;
    sendwordEditController.delegate = self;
    [self presentViewController:sendwordEditController animated:NO completion:nil];
}

- (void)clickDoneWithType:(TaskEditType)type text:(NSString *)text
{
    if (type == TaskEditTypeOfDescription) {
        _pushTaskView.descriptionText.text = text;
    }else if (type == TaskEditTypeOfSendWord){
        _pushTaskView.sendWordText.text = text;

    }
}

- (void)clickTrueWithName:(NSString *)name description:(NSString *)description sendWord:(NSString *)sendWord giftImage:(NSString *)image
{
    _taskModel.name = name;
    _taskModel.descriptionText = [NSString metaEmptyString:description];
    _taskModel.sendwordText = [NSString metaEmptyString:sendWord];
    _taskModel.image = [NSString metaEmptyString:image];
    
    _dateModel.taskDays = [ZZDateFormat fetchNumberOfStartDate:_dateModel.startDate endDate:_dateModel.endDate];
    
    
    
    
    if ([NSString isEmptyString:_taskModel.name]) {
        [YJProgressHud showErrorHud:@"必须设置一个任务名字~~~"];
    }else if ([NSString isEmptyString:_taskModel.image]){
        [YJProgressHud showErrorHud:@"必须设置一张奖励物品图片哦~~~"];
    }else if (_friendModel == nil){
        [YJProgressHud showErrorHud:@"必须选择执行人哦~~~"];
    }else if (_dateModel.taskDays > 30){
        [YJProgressHud showErrorHud:@"目前任务周期只能30天内哦~~~"];
    } else{
        [YJProgressHud showLoadingHud:@"正在上传..."];
        [_dataCenter pushTaskWithTaskModel:_taskModel friendModel:_friendModel dateModel:_dateModel complation:^(id error) {
            if (error == nil) {
                [YJProgressHud showSuccessHud:@"上传成功"];
                [self dismissViewControllerAnimated:NO completion:nil];
            }else{
                [YJProgressHud showErrorHud:error];
            }
        }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
